#include "timer.h"
#include "sys.h"
//通用定时器3中断初始化
//这里时钟选择为APB1的2倍，而APB1为36M
//arr：自动重装值。
//psc：时钟预分频数
//这里使用的是定时器3!
void TIM3_Int_Init(u16 arr,u16 psc)
{
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
	
	//定时器TIM3初始化
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
 
	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断

	//中断优先级NVIC设置
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器


	TIM_Cmd(TIM3, ENABLE);  //使能TIMx					 
}

//定时器3中断服务程序

int TIM3_CNT = 0;
int Update_Time = 10;

void TIM3_IRQHandler(void)   //TIM3中断
{
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
	{
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  //清除TIMx更新中断标志 
		//---------------USER-------------------
		
		if(TIM3_CNT == Update_Time)
		{
			
			
			TIM3_CNT = 0;
		}
		else
		{
			
			TIM3_CNT++;
		}
	}
}


//-------------------------------------------------------------
//输入捕捉
//-------------------------------------------------------------

//定时器4通道4输入捕获配置
TIM_ICInitTypeDef  TIM4_ICInitStructure;

void TIM4_Cap_Init(u16 arr,u16 psc)
{	 
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
   	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);						//使能TIM4时钟
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);  						//使能GPIOB时钟
	
	GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9;  								//PB9 清除之前设置  
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 								//PB9 输入上拉
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	//GPIO_SetBits(GPIOB,GPIO_Pin_9);						 						//PB9 
	
	//初始化定时器4 TIM4	 
	TIM_TimeBaseStructure.TIM_Period = arr; 									//设定计数器自动重装值 
	TIM_TimeBaseStructure.TIM_Prescaler =psc; 									//预分频器   
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 					//设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  				//TIM向上计数模式
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 							//根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  
	//初始化TIM4输入捕获参数
	TIM4_ICInitStructure.TIM_Channel = TIM_Channel_4; 							//CC1S=01 	选择输入端 IC1映射到TI1上
  	TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;				//下降沿捕获
  	TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; 			//映射到TI1上
  	TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;	 					//配置输入分频,不分频 
  	TIM4_ICInitStructure.TIM_ICFilter = 0x00;									//IC1F=0000 配置输入滤波器 不滤波
  	TIM_ICInit(TIM4, &TIM4_ICInitStructure);
	
	//中断分组初始化
	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  							//TIM4中断
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  					//先占优先级2级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  						//从优先级0级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 							//IRQ通道被使能
	NVIC_Init(&NVIC_InitStructure);  											//根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 
	
	TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC4,ENABLE);							//允许更新中断 ,允许CC1IE捕获中断	
	
   	TIM4_Enable(); 																//使能定时器4
}

u8  TIM4CH4_CAPTURE_RUN = 2;

u8  TIM4CH4_CAPTURE_FIRST=0;	//输入捕获状态		    				
u8  TIM4CH4_CAPTURE_TIMES=0;	//输入捕获跳变次数
u16 TIM4CH4_CAPTURE_ARRAY[256];

void TIM4_Enable(void)
{
	TIM4CH4_CAPTURE_FIRST = 0;
	TIM4CH4_CAPTURE_TIMES = 0;
	
	TIM4CH4_CAPTURE_RUN = 1;
	//TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Falling);							//CC1P=1 设置为下降沿捕获
	
  	TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;				//下降沿捕获
  	TIM_ICInit(TIM4, &TIM4_ICInitStructure);
	
	TIM_Cmd(TIM4,ENABLE);
}

void TIM4_Disable(void)
{
	TIM_Cmd(TIM4,DISABLE);
}


u16 cnt=0;

//定时器5中断服务程序	 
void TIM4_IRQHandler(void)
{ 
	if(TIM4CH4_CAPTURE_RUN==1)		
	{	  
		//计数器溢出了，则说明捕获结束
		if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
		{	    
			//error
			if(TIM4CH4_CAPTURE_TIMES==1 && TIM_GetCapture4(TIM4)>20000)
			{
				TIM4CH4_CAPTURE_FIRST = 0;
				TIM4CH4_CAPTURE_TIMES = 0;
			}
			
			if(TIM4CH4_CAPTURE_TIMES==199)
				TIM4CH4_CAPTURE_RUN = 0;
		}
	
		//跳变捕捉
		if (TIM_GetITStatus(TIM4, TIM_IT_CC4) != RESET)					//捕获1发生捕获事件
		{	
			//第一次下降沿触发
			if(TIM4CH4_CAPTURE_FIRST==0)
			{
				TIM4CH4_CAPTURE_FIRST=1;
	 			TIM_SetCounter(TIM4,0);
		   	
				TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;		
				TIM_ICInit(TIM4, &TIM4_ICInitStructure);
			}
			else 
			{
				cnt = TIM_GetCapture4(TIM4);
				if(TIM4CH4_CAPTURE_TIMES==0 && cnt<4000)//判断头过滤错误
				{
					TIM4CH4_CAPTURE_FIRST = 0;
					TIM_SetCounter(TIM4,0);
					TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;		
					TIM_ICInit(TIM4, &TIM4_ICInitStructure);
				}
				else 
				{
					TIM4CH4_CAPTURE_ARRAY[TIM4CH4_CAPTURE_TIMES] = cnt;
					TIM_SetCounter(TIM4,0);
					
					//偶数捕获上升沿
					if(TIM4CH4_CAPTURE_TIMES%2 == 0)
					{
						TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;		
						TIM_ICInit(TIM4, &TIM4_ICInitStructure);
					}
					else
					{
						TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;		
						TIM_ICInit(TIM4, &TIM4_ICInitStructure);
					}
					TIM4CH4_CAPTURE_TIMES ++;
				}
		
			}
					    
		}			     	    					   
 	}
 
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC4|TIM_IT_Update); //清除中断标志位
 
}


